const express = require('express');
const router = express.Router();
const article = require('../db/article');



router.get('/wx/list/:page', (req, res) => {
    //页码
    let page = req.params.page;
    //每页数量
    let pageNum = 10;
    article.count({}, function (err, counts) {
        //总页数
        let PageTotal = (Math.ceil(counts / pageNum));

        if(PageTotal==0)
        {
            res.json('blog/index.html',{
                status:false,
                msg:'内容为空'
            });
            return;
        }

        article.find({}).limit(pageNum).skip((page - 1) * pageNum).exec((err, data) => {
            if (!err) {
                res.json( {
                    status:true,
                    data: data
                })
            }
        })
    });
});




router.get('/wx/article/:id', (req, res) => {
    let id = req.params.id;
    article.find({_id: id}, function (err, data) {
        if (!err) {
            if (data.length) {
                data[0].num += 1;
                data[0].save();
                res.json({
                    status:true,
                    data: data[0],
                })

            }
            else {
                res.json({
                    status:false,
                    msg:'没找到这篇文章哟~'
                })
            }
        }
        else {
            res.json({status:false,msg:err});
        }
    });
});

module.exports=router;